package cn.gorit.admin.service;

import cn.gorit.common.enums.ResultEnums;
import cn.gorit.common.enums.StateEnums;
import cn.gorit.common.exception.AdminException;
import cn.gorit.common.exception.SystemException;
import cn.gorit.common.lang.Result;
import cn.gorit.dto.system.LinkDto;
import cn.gorit.dto.system.TimeLineDto;
import cn.gorit.entity.system.Link;
import cn.gorit.entity.system.MsgBoard;
import cn.gorit.entity.system.TimeLine;
import cn.gorit.mapper.system.LinkMapper;
import cn.gorit.mapper.system.MsgBoardMapper;
import cn.gorit.mapper.system.TimeLineMapper;
import cn.gorit.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version v1.0
 * @Classname SystemService
 * @Description TODO
 * @Author CodingGorit
 * @Created on 2021/4/16 0:21
 * @IDLE IntelliJ IDEA
 **/
@Service
public class SystemService {

    // 友情链接相关
    @Autowired
    private LinkMapper linkMapper;

    @Autowired
    private MsgBoardMapper msgBoardMapper;

    // 时间轴
    @Autowired
    private TimeLineMapper timeLineMapper;

    /**
     * 添加 link
     * @param link
     */
    public void saveLink(LinkDto link) {
        linkMapper.saveLink(link);
    }

    /**
     * 修改
     *
     * @param link
     */
    public void updateLink(Link link) {
        linkMapper.updateLink(link);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public Link getLinkById(Integer id) {
        Link linkById = linkMapper.getLinkById(id);
        if (linkById != null)
            return linkById;
        throw new AdminException(ResultEnums.PARAMS_ERROR.getCode(),ResultEnums.PARAMS_ERROR.getMsg());
    }

    /**
     * 查询所有
     *
     * @return
     */
    public List<Link> getLinkAll() {
        return linkMapper.getAllLink();
    }

    /**
     * 根据id删除
     * @param id
     */
    public void deleteLinkById(Integer id) {
        linkMapper.deleteLinkById(id);
    }

    // ============== 留言版相关
    public Result<Page<MsgBoard>> getBroadByPage(Page<MsgBoard> page) {
        List<MsgBoard> list = msgBoardMapper.getBroadByPage(page);
        page.setList(list);
        Integer count = getBroadPageCount(page);
        page.setTotalCount(count);
        return new Result<>(ResultEnums.QUERY_SUCCESS.getCode(),ResultEnums.QUERY_SUCCESS.getMsg(),page);
    }

    public Integer getBroadPageCount(Page<MsgBoard> page) {
        return msgBoardMapper.getMsgBoardCountByPage(page);
    }

    public Result<Object> updateMsgBoard(MsgBoard msgBoard) {
        msgBoardMapper.update(msgBoard);
        return new Result<>(ResultEnums.UPDATE_SUCCESS.getCode(),ResultEnums.UPDATE_SUCCESS.getMsg(),"");
    }

    public Result<MsgBoard> getMsgBoardById(Integer id) {
        return new Result<MsgBoard>(ResultEnums.QUERY_SUCCESS.getCode(),ResultEnums.QUERY_SUCCESS.getMsg(),msgBoardMapper.getById(id));
    }

    public Result<Object> updateBroadStatus(Integer status, Integer id) {
        if (status.equals(2)) {
            msgBoardMapper.updateStatus(StateEnums.REPLYED.getCode(),id);
        }
        if (status.equals(1)) {
            msgBoardMapper.updateStatus(StateEnums.ALLOWED.getCode(),id);
        }
        if (status.equals(0)) {
            msgBoardMapper.updateStatus(StateEnums.CHECKED.getCode(),id);
        }
        if (status.equals(-1)) {
            msgBoardMapper.updateStatus(StateEnums.LOGIC_DELETED.getCode(),id);
        }
        return new Result<>(ResultEnums.UPDATE_SUCCESS.getCode(),ResultEnums.UPDATE_SUCCESS.getMsg(),"");
    }

    // 时间轴相关 后台 api

    public Result<Object> addTimeLine(TimeLineDto dto) {
        timeLineMapper.addTimeLine(dto);
        return new Result<>(ResultEnums.ADD_SUCCESS.getCode(),ResultEnums.ADD_SUCCESS.getMsg(),"");
    }

    public Result<Object> enableTimeLine(Integer id, Integer enable) {
        String msg = "";
        if (enable.equals(1)) {
            timeLineMapper.updateTimeLineEnabled(id, StateEnums.ENABLED.getCode());
            msg = StateEnums.ENABLED.getMsg();
        }
        if (enable.equals(0)) {
            timeLineMapper.updateTimeLineEnabled(id,StateEnums.DISABLE.getCode());
            msg = StateEnums.DISABLE.getMsg();
        }
        return new Result<>(ResultEnums.UPDATE_SUCCESS.getCode(),msg,"");
    }

    public Result<Object> updateTimeLine(TimeLine dto) {
        // 参数校验
        if (dto.getVersion() == null)
            throw new SystemException(ResultEnums.UPDATE_FAILED.getCode(),ResultEnums.UPDATE_FAILED.getMsg());
        timeLineMapper.updateTimeLine(dto);
        return new Result<>(ResultEnums.UPDATE_SUCCESS.getCode(),ResultEnums.UPDATE_SUCCESS.getMsg(),"");
    }

    // 后台分页
    public Result<Page<TimeLine>> getTimeLineByPage(Page<TimeLine> page) {
        List<TimeLine> list = timeLineMapper.getTimeLineByPage(page);
        page.setList(list);
        int count = getTimeLineByPageCount(page);
        page.setTotalCount(count);
        return new Result<>(ResultEnums.QUERY_SUCCESS.getCode(),ResultEnums.QUERY_SUCCESS.getMsg(),page);
    }

    public Integer getTimeLineByPageCount(Page<TimeLine> page) {
        return timeLineMapper.getTimeLineCountByPage(page);
    }

    // 根据 ID 获取时间轴
    public Result<Object> getTimeLineById(Integer id) {
        return new Result<>(ResultEnums.QUERY_SUCCESS.getCode(),ResultEnums.QUERY_SUCCESS.getMsg(),timeLineMapper.getTimeLineById(id));
    }
}
